<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="80px" inline>
      <el-form-item  label="用户姓名">
        <el-input style="width: 80%;"  v-model="form.nickName"></el-input>
      </el-form-item>
      <el-form-item label="角色" >
        <el-select v-model="form.role" multiple placeholder="请选择">
          <el-option
            v-for="item in roleOptions"
            :key="item.value"
            :label="item.name"
            :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item  label="用户状态">
        <el-select v-model="form.status" placeholder="请选择">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit(form)">查询</el-button>
        <el-button  @click="onReset(form)">重置</el-button>
        <el-button type="success" @click="showDia('', 'add')">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        type="index"
        width="30">
      </el-table-column>
      <el-table-column align="center" label="用户姓名" width="95">
        <template slot-scope="scope">
          {{ scope.row.nickName }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="登录名" width="95">
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column label="性别" width="80"  align="center">
        <template slot-scope="scope">
          {{ scope.row.sex }}
        </template>
      </el-table-column>
      <el-table-column label="角色" width="150" align="center">
        <template slot-scope="scope">
          <ul v-for="item in scope.row.showRoleList">
            <span>{{ item }}</span>
          </ul>
        </template>
      </el-table-column>
      <el-table-column label="工号" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.userNo }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" width="130" align="center">
        <template slot-scope="scope">
          {{ scope.row.mobile }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="80" align="center">
        <template slot-scope="scope" >
          <span v-if="scope.row.status === 0">冻结</span>
          <span v-if="scope.row.status === 1">正常</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="350">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="showDia(scope.row.id, 'look')">查看</el-button>
          <el-button
            size="mini"
            @click="showDia(scope.row.id, 'edit')">编辑
          </el-button>
          <el-popconfirm title="确定删除吗？" @onConfirm="handleDelete(scope.row.id)">
            <el-button size="mini" type="danger" slot="reference">删除</el-button>
          </el-popconfirm>
          <el-popconfirm title="确定冻结吗？" @onConfirm="handleFrozen(scope.row.id)">
            <el-button :disabled="scope.row.status === 0 "  size="mini" type="warning" slot="reference">冻结</el-button>
          </el-popconfirm>
          <el-popconfirm title="确定解冻吗？" @onConfirm="handleUnfrozen(scope.row.id)">
            <el-button :disabled="scope.row.status === 1 " size="mini" type="success" slot="reference">解冻</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="margin-top: 40px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="100"
        layout="prev, pager, next, jumper"
        :total="1000">
      </el-pagination>
    </div>
    <el-dialog title="详情" :visible.sync="dialogFormVisible">
      <el-form ref="userDetail"  label-width="80px" :disabled="diaLook" inline>
        <el-form-item  label="用户姓名">
          <el-input  style="width: 80%;"  v-model="userDetail.nickName"></el-input>
        </el-form-item>
        <el-form-item  label="登录名">
          <el-input  style="width: 80%;"  v-model="userDetail.username"></el-input>
        </el-form-item>
        <el-form-item  label="工号">
          <el-input style="width: 80%;"  v-model="userDetail.userNo"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input style="width: 80%;"  v-model="userDetail.mobile"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="userDetail.showRoleList" multiple placeholder="请选择">
            <el-option
              v-for="item in roleOptions"
              :key="item.value"
              :label="item.name"
              :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-input style="width: 80%;" disabled v-model = "userDetail.status ===0?'冻结':'正常'"></el-input>
        </el-form-item>
        <el-form-item label="性别" label-width="80px">
          <el-select v-model="userDetail.sex" placeholder="请选择">
            <el-option
              v-for="item in genderOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="onCancle">取 消</el-button>
        <el-button type="primary" @click="onUpdate(userDetail)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUserList, updateUser, addUser, getUserById, deleteUserById, frozenUserById, unfrozenUserById } from '@/api/user'
import { allRoleList } from '@/api/role'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: false,
      form: {},
      userDetail: {},
      currentPage: 1,
      dialogFormVisible: false,
      diaLook: true,
      operate: '',
      roleOptions: [],
      statusOptions: [{
        value: 0,
        label: '冻结'
      }, {
        value: 1,
        label: '正常'
      }],
      genderOptions: [{
        value: '男',
        label: '男'
      }, {
        value: '女',
        label: '女'
      }]
    }
  },
  created() {
    this.fetchData(this.form)
    this.allRoleList()
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.form.pageNum = val
      getUserList(this.form).then(res => {
        this.list = res.data.rows
      })
    },
    handleEdit(row) {
    },
    showDia(id, operate) {
      this.operate = operate
      if (operate === 'look') {
        getUserById(id).then(res => {
          this.userDetail = res.data
          this.diaLook = true
        })
      } else if (operate === 'edit') {
        getUserById(id).then(res => {
          this.userDetail = res.data
          this.diaLook = false
        })
      } else if (operate === 'add') {
        this.userDetail = {}
        this.diaLook = false
      }
      this.dialogFormVisible = true
    },
    handleDelete(id) {
      deleteUserById(id).then(res => {
        this.onSubmit(this.form)
      })
    },
    handleFrozen(id) {
      frozenUserById(id).then(res => {
        this.onSubmit(this.form)
      })
    },
    handleUnfrozen(id) {
      unfrozenUserById(id).then(res => {
        this.onSubmit(this.form)
      })
    },
    onSubmit(param) {
      getUserList(param).then(res => {
        this.list = res.data.rows
      })
      // this.fetchData(this.form)
    },
    onReset(form) {
      this.form = {}
    },
    onCancle() {
      this.dialogFormVisible = false
      this.userDetail = {}
    },
    onUpdate(data) {
      if (this.diaLook === true) {
        this.dialogFormVisible = false
        this.userDetail = {}
        return
      }
      if (this.operate === 'add') {
        addUser(data).then(res => {
          this.dialogFormVisible = false
          this.onSubmit(this.form)
          this.equipmentDetail = {}
        })
      } else if (this.operate === 'edit') {
        updateUser(data).then(res => {
          this.dialogFormVisible = false
          this.onSubmit(this.form)
          this.userDetail = {}
        })
      }
      this.operate = ''
    },
    fetchData(param) {
      getUserList(param).then(res => {
        this.list = res.data.rows
      })
    },
    allRoleList() {
      allRoleList().then(res => {
        this.roleOptions = res.data
      })
    }
  }
}
</script>
